Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Xml
Imports System.Xml.Xsl

Partial Public Class UserCuntrols_CustomDropDown
    Inherits System.Web.UI.UserControl

    Private _ItemStack As New Stack
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            _ItemStack.Push(_ParentID)
            ViewState("ItemStack") = _ItemStack
            BindData()
        End If
        If Request.Browser.Browser = "IE" Then
            imgDropdown.Style.Add("margin-top", "-5px")
        End If

        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "mootools", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/mootools.js")))
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "pedro", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/pedro.js")))
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "element", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/element.js")))
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "event", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/event.js")))
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "pane", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/pane.js")))
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "global-nav", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/global-nav.js")))

    End Sub

    Protected _ParentID As String = "Root"
    Private _DataXML As String
    Public Property DataXML() As String
        Get
            Return _DataXML
        End Get
        Set(ByVal value As String)
            _DataXML = value
        End Set
    End Property
    Public ReadOnly Property SelectedText() As String
        Get
            Return txtDropdown.Text
        End Get
    End Property
    Public ReadOnly Property SelectedValue() As String
        Get
            Return hidDropdown.Value
        End Get
    End Property

    Private Sub BindData()
        Dim oXmlDocument As New XmlDocument()
        oXmlDocument.Load(Server.MapPath(_DataXML))
        CallRecursive(oXmlDocument.ChildNodes(1))
        If ViewState("ItemStack") IsNot Nothing Then
            _ItemStack = ViewState("ItemStack")
        End If
        If _ItemStack.Count > 1 Then
            lnkBack.Visible = True
            lnkBackMain.Visible = True
        Else
            lnkBack.Visible = False
            lnkBackMain.Visible = False
        End If
    End Sub
    Private Sub CallRecursive(ByVal oXml As XmlElement)
        Dim i As Integer
        Dim j As Integer
        For i = 0 To oXml.ChildNodes.Count - 1
            If _ParentID = "Root" Then
                txtDropdown.Text = " -- Select -- "
                hidDropdown.Value = ""
                litHeading.Text = ""
                Dim CategoryArray As New ArrayList()
                For j = 0 To oXml.ChildNodes(i).ChildNodes.Count - 1
                    Dim Temp As New Category()
                    Temp.CategoryID = oXml.ChildNodes(i).ChildNodes(j).Attributes("id").Value
                    Temp.Text = oXml.ChildNodes(i).ChildNodes(j).Attributes("text").Value
                    Temp.Href = oXml.ChildNodes(i).ChildNodes(j).Attributes("href").Value
                    Temp.Image = oXml.ChildNodes(i).ChildNodes(j).Attributes("image").Value
                    Temp.Target = oXml.ChildNodes(i).ChildNodes(j).Attributes("target").Value

                    CategoryArray.Add(Temp)
                Next
                CategoryArray.Sort()
                dlSubCategory.DataSource = CategoryArray
                dlSubCategory.DataBind()
                If CategoryArray.Count > 0 Then
                    litMessage.Text = ""
                Else
                    litMessage.Text = "No data found."
                End If

            ElseIf oXml.ChildNodes(i).Attributes IsNot Nothing And oXml.ChildNodes(i).Attributes.Count > 0 And oXml.ChildNodes(i).Attributes("id") IsNot Nothing And oXml.ChildNodes(i).Attributes("id").Value = _ParentID Then
                litHeading.Text = oXml.ChildNodes(i).Attributes("text").Value
                txtDropdown.Text = litHeading.Text
                hidDropdown.Value = txtDropdown.Text
                Dim CategoryArray As New ArrayList()
                For j = 0 To oXml.ChildNodes(i).ChildNodes.Count - 1
                    Dim Temp As New Category()
                    Temp.CategoryID = oXml.ChildNodes(i).ChildNodes(j).Attributes("id").Value
                    Temp.Text = oXml.ChildNodes(i).ChildNodes(j).Attributes("text").Value
                    Temp.Href = oXml.ChildNodes(i).ChildNodes(j).Attributes("href").Value
                    Temp.Image = oXml.ChildNodes(i).ChildNodes(j).Attributes("image").Value
                    Temp.Target = oXml.ChildNodes(i).ChildNodes(j).Attributes("target").Value

                    CategoryArray.Add(Temp)
                Next
                CategoryArray.Sort()
                dlSubCategory.DataSource = CategoryArray
                dlSubCategory.DataBind()
                If CategoryArray.Count > 0 Then
                    litMessage.Text = ""
                Else
                    litMessage.Text = "No data found."
                End If
                Exit For
            Else
                CallRecursive(oXml.ChildNodes(i))
            End If
        Next
    End Sub

    Public Class Category
        Implements IComparable

        Private _CategoryID As String
        Property CategoryID() As String
            Get
                Return _CategoryID
            End Get
            Set(ByVal value As String)
                _CategoryID = value
            End Set
        End Property

        Private _Text As String
        Property Text() As String
            Get
                Return _Text
            End Get
            Set(ByVal value As String)
                _Text = value
            End Set
        End Property
        Private _Href As String
        Property Href() As String
            Get
                Return _Href
            End Get
            Set(ByVal value As String)
                _Href = value
            End Set
        End Property
        Private _Target As String
        Property Target() As String
            Get
                Return _Target
            End Get
            Set(ByVal value As String)
                _Target = value
            End Set
        End Property
        Private _Image As String
        Property Image() As String
            Get
                Return _Image
            End Get
            Set(ByVal value As String)
                _Image = value
            End Set
        End Property

        Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
            Dim _Category As Category = DirectCast(obj, Category)
            Return String.Compare(Me.Text, _Category.Text)
        End Function
    End Class

    Protected Function PopulateImage(ByVal ImageUrl As String) As String
        Dim ImageTag As String = "<img src=""" & ResolveUrl(ImageUrl) & """ border=""0"" />"
        If ImageUrl.Length = 0 Then
            ImageTag = ""
        End If
        Return ImageTag
    End Function

    Protected Sub lnkBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBack.Click
        If ViewState("ItemStack") IsNot Nothing Then
            _ItemStack = ViewState("ItemStack")
        End If
        _ParentID = _ItemStack.Pop().ToString()
        _ParentID = _ItemStack.Pop().ToString()
        _ItemStack.Push(_ParentID)
        ViewState("ItemStack") = _ItemStack
        BindData()
    End Sub

    Protected Sub lnkBackMain_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBackMain.Click
        _ParentID = "Root"
        _ItemStack.Clear()
        _ItemStack.Push(_ParentID)
        ViewState("ItemStack") = _ItemStack
        BindData()
    End Sub

    Protected Sub dlSubCategory_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlSubCategory.ItemCommand
        If e.CommandName = "SelectCategory" Then
            _ItemStack = ViewState("ItemStack")
            _ParentID = e.CommandArgument.ToString()
            _ItemStack.Push(_ParentID)
            ViewState("ItemStack") = _ItemStack
            BindData()
        End If
    End Sub
End Class
